﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;


namespace nhahangbarota
{
    public partial class DoanhThu : Form
    {
        public DoanhThu()
        {
            InitializeComponent();
            KhoiTaoGiaoDien();
        }

        private void DoanhThu_Load(object sender, EventArgs e)
        {
            LoadKhoangThoiGian();
            //LoadDoanhThu();
        }

        void KhoiTaoGiaoDien()
        {
            dgvDoanhThu.Columns["colMaDT"].ReadOnly = true;
            dgvDoanhThu.Columns["colNgay"].ReadOnly = true;
            dgvDoanhThu.Columns["colBuoi"].ReadOnly = true;
            dgvDoanhThu.Columns["colTienGoc"].ReadOnly = true;
            dgvDoanhThu.Columns["colSauThue"].ReadOnly = true;
            dgvDoanhThu.Columns["colConLai"].ReadOnly = true;
            dgvDoanhThu.Columns["colNgay"].DefaultCellStyle.Format = "dd/MM/yyyy";
            dgvDoanhThu.Columns["colGiamGia"].DefaultCellStyle.BackColor = Color.Beige;
            dgvDoanhThu.Columns["colSoHD"].DefaultCellStyle.BackColor = Color.Beige;

            dgvHoaDon.Columns["colMaHD"].ReadOnly = true;
            dgvHoaDon.Columns["colNgayHD"].ReadOnly = true;
            dgvHoaDon.Columns["colTenKhach"].ReadOnly = true;
            dgvHoaDon.Columns["colMaNV"].ReadOnly = true;
            dgvHoaDon.Columns["colTongTien"].ReadOnly = true;
            dgvHoaDon.Columns["colBuoiHD"].ReadOnly = true;
            dgvHoaDon.Columns["colNgayHD"].DefaultCellStyle.Format = "dd/MM/yyyy";
        }

        public void LoadDoanhThu()
        {
            DoanhThuBUS BUS = new DoanhThuBUS();
            dgvDoanhThu.DataSource = BUS.LoadDoanhThu();
        }

        public void LoadKhoangThoiGian()
        {
            List<string> dsThoiGian = new List<string>();
            dsThoiGian.Add("1 ngày");
            dsThoiGian.Add("3 ngày");
            dsThoiGian.Add("10 ngày");
            dsThoiGian.Add("20 ngày");
            dsThoiGian.Add("40 ngày");
            dsThoiGian.Add("Tất cả");
            cboKTGian.DataSource = dsThoiGian;
            cboKTGian.SelectedItem = "1 ngày";
        }
        
        private void cboKTGian_SelectedIndexChanged(object sender, EventArgs e)
        {
            string _text = cboKTGian.SelectedValue.ToString();
            DoanhThuBUS BUS = new DoanhThuBUS();
            int _numb = SoLuaChon(_text);
            if (_numb != -1)
                dgvDoanhThu.DataSource = BUS.LoadDoanhThuTop(_numb);
        }

        int SoLuaChon(string _text)
        {
            switch (_text)
            {
                case "1 ngày":
                    return 2;
                case "3 ngày":
                    return 6;
                case "10 ngày":
                    return 20;
                case "20 ngày":
                    return 40;
                case "40 ngày":
                    return 80;
                case "Tất cả":
                    return 0;
                default: return -1;
            }
        }


        private void dgvDoanhThu_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            DoanhThuDTO dtdto = new DoanhThuDTO();
            dtdto.MaDT = int.Parse(dgvDoanhThu.Rows[e.RowIndex].Cells["colMaDT"].Value.ToString());
            DoanhThuBUS BUS = new DoanhThuBUS();

            if (e.ColumnIndex == 6)
            {
                dtdto.SauThue = float.Parse(dgvDoanhThu.Rows[e.RowIndex].Cells["colSauThue"].Value.ToString());
                dtdto.GiamGia = float.Parse(dgvDoanhThu.Rows[e.RowIndex].Cells["colGiamGia"].Value.ToString());
                dtdto.ConLai = dtdto.SauThue - dtdto.GiamGia;
                dgvDoanhThu.Rows[e.RowIndex].Cells["colConLai"].Value = dtdto.ConLai;
                //Update vo CSDL
                BUS.UpdateGiamGia_ConLai(dtdto.MaDT, dtdto.GiamGia, dtdto.ConLai);
            }
            else if (e.ColumnIndex == 3)
            {
                dtdto.SoHoaDon = dgvDoanhThu.Rows[e.RowIndex].Cells["colSoHD"].Value.ToString();
                //Update vo CSDL
                BUS.UpdateSoHoaDon(dtdto.MaDT, dtdto.SoHoaDon);
            }
        }

        private void btnNapDuLieuMoi_Click(object sender, EventArgs e)
        {
            NapDuLieuMoi();
        }

        void NapDuLieuMoi()
        {
            DoanhThuBUS BUS = new DoanhThuBUS();
            DataTable table = new DataTable();
            BUS.NapDuLieuMoi();

            string _text = cboKTGian.SelectedValue.ToString();
            int _numb = SoLuaChon(_text);
            if (_numb != -1)
                dgvDoanhThu.DataSource = BUS.LoadDoanhThuTop(_numb);
        }

        private void dgvDoanhThu_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            MessageBox.Show("Tại đây, bạn phải nhập số.","Message");
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            tbxMaDT.Focus();
            int _maDT;
            string _text = tbxMaDT.Text;
            if (int.TryParse(_text, out _maDT) == true)
            {
                DataTable table = LoadHoaDonTheoMaDT(_maDT);
                if (table.Rows.Count != 0)
                    dgvHoaDon.DataSource = LoadHoaDonTheoMaDT(_maDT);
                else
                {
                    dgvHoaDon.DataSource = table;
                    MessageBox.Show("Không có kết quả", "Message");
                }
            }
            else
            {
                MessageBox.Show("Bạn phải nhập đúng mã hóa đơn", "Message");
            }
        }
        DataTable LoadHoaDonTheoMaDT(int _maDT)
        {
            DataTable table = new DataTable();
            table.Columns.Add("MaHD", typeof(int));
            table.Columns.Add("NgayHD", typeof(DateTime));
            table.Columns.Add("TenKhach", typeof(string));
            table.Columns.Add("MaNV", typeof(int));
            table.Columns.Add("TongTien", typeof(float));
            table.Columns.Add("BuoiHD", typeof(string));

            //Lay tu CSDL
            DoanhThuBUS BUS = new DoanhThuBUS();
            DataTable t_table = new DataTable();
            t_table = BUS.LoadHoaDonTheoMaDT(_maDT);
            //Edit no lai cot colBuoiHD
            for (int i = 0; i < t_table.Rows.Count; i++)
            {
                DataRow row = table.NewRow();
                DataRow t_row = t_table.Rows[i];
                row["MaHD"] = t_row["MaHoaDon"];
                row["NgayHD"] = t_row["Ngay"];
                row["TenKhach"] = t_row["TenKhach"];
                row["MaNV"] = t_row["MaNV"];
                row["TongTien"] = t_row["TongTien"];

                int n;
                string _text = t_row["Buoi"].ToString();
                if (int.TryParse(_text, out n) == true)
                {
                    if (n == 0)
                        row["BuoiHD"] = "Sáng";
                    else if (n == 1)
                        row["BuoiHD"] = "Tối";
                }                
                table.Rows.Add(row);
                
            }
            return table;
        }

        private void tPageBangDT_Click(object sender, EventArgs e)
        {

        }


    }
}
//Chi can boi den cac dong lenh duoi day va F5(Sql) se tu dong tao table DoanhThu, du lieu DoanhThu va cac Procedure can thiet.(update lan 2)
/*
use ROBATA
go
----------
create table DoanhThu(
MaDT int,
Ngay datetime,
Buoi nvarchar(50),
SoHoaDon nvarchar(50),
TienGoc float,
SauThue float,
GiamGia float,
ConLai float,
constraint pk_MaDT primary key(MaDT)
)
go

--------------------------
--alter proc usp_MaDT_DoanhThu
create proc usp_MaDT_DoanhThu
as
declare @index int
set @index=1
declare cur_MaDT cursor
for select MaDT from DoanhThu
open cur_MaDT
declare @MaDT int
fetch next from cur_MaDT into @MaDT
while @@FETCH_STATUS=0
begin
	if(@index!=@MaDT)
	begin
		break
	end
	else
		set @index = @index +1
fetch next from cur_MaDT into @MaDT
end
close cur_MaDT
deallocate cur_MaDT
return @index
go
---
--declare @chiso int
--exec @chiso=usp_MaDT_DoanhThu
--print @chiso
--go
------------------------------------------------------
create proc usp_DoanhThuMoi
as
--B1 Xoa DoanhThu ma khong ton tai HoaDon: phai co usp_LoadHoaDonTheoMaDT
declare cur_XoaDT cursor
for select MaDT,Ngay,Buoi from DoanhThu
open cur_XoaDT
declare @MaDT_Xoa int, @MaDT_PhaiXoa int
set @MaDT_PhaiXoa=-1
declare @NgayDT_Xoa datetime, @BuoiDT_Xoa nvarchar(50), @BuoiDT2_Xoa int
fetch next from cur_XoaDT into @MaDT_Xoa, @NgayDT_Xoa, @BuoiDT_Xoa
while @@FETCH_STATUS=0
begin
	if(@BuoiDT_Xoa=N'Sáng')
	set @BuoiDT2_Xoa=0
	else if(@BuoiDT_Xoa=N'Tối')
	set @BuoiDT2_Xoa=1	
	if(@MaDT_PhaiXoa!=-1)
	begin
		delete from DoanhThu where MaDT=@MaDT_PhaiXoa
		set @MaDT_PhaiXoa=-1
	end
	if not exists(select*from HoaDon where Ngay=@NgayDT_Xoa and Buoi=@BuoiDT2_Xoa)
	begin
		set @MaDT_PhaiXoa=@MaDT_Xoa
	end
fetch next from cur_XoaDT into @MaDT_Xoa, @NgayDT_Xoa, @BuoiDT_Xoa
end
close cur_XoaDT
deallocate cur_XoaDT
delete from DoanhThu where MaDT=@MaDT_PhaiXoa
--B2 Cap nhat DoanhThu
declare @StepNgay datetime, @SumTongTien float, @StepBuoi nvarchar(50)
set @StepNgay=GETDATE()
set @SumTongTien=0
set @StepBuoi=''

declare cur_HD cursor
for select Ngay,TongTien,Buoi from HoaDon
open cur_HD
declare @Ngay datetime, @TongTien float, @Buoi int
fetch next from cur_HD into @Ngay,@TongTien,@Buoi
declare @flag_kt int
set @flag_kt = 1
while @@FETCH_STATUS=0
begin
	if(@flag_kt=1)
	begin
		set @flag_kt=0
	end
	else if(DATEDIFF(day,@Ngay,@StepNgay)=0 and ((@StepBuoi=N'Sáng'and @Buoi=0)or(@StepBuoi=N'Tối'and @Buoi=1)))
		begin
		fetch next from cur_HD into @Ngay,@TongTien,@Buoi
		continue
		end
	set @StepNgay=@Ngay
	select @SumTongTien=SUM(TongTien) from HoaDon h	where h.Ngay=@Ngay and h.Buoi=@Buoi
	--tao new row tron DoanhThu
	declare @MaDT int, @SoHoaDon nvarchar(50), @SauThue float, @GiamGia float, @ConLai float
	exec @MaDT = usp_MaDT_DoanhThu
	if(@Buoi=0)
		set @StepBuoi=N'Sáng'
	else if(@Buoi=1)
		set @StepBuoi=N'Tối'	
	set @SoHoaDon='~'
	declare @t float
	set @t= (select GT from ThamSo)+1
	set @SauThue=round(@t*@SumTongTien,0)
	set @GiamGia=0
	set @ConLai=round(@SauThue-@GiamGia,0)
		
	declare @temp0 nvarchar(50), @temp1 float, @temp2 float
	select @temp0=SoHoaDon, @temp1=GiamGia,@temp2=ConLai from DoanhThu where DATEDIFF(day,@Ngay,Ngay)=0 and Buoi=@StepBuoi
	if @temp2 is null
		insert into DoanhThu values(@MaDT,@StepNgay,@StepBuoi,@SoHoaDon,@SumTongTien,@SauThue,@GiamGia,@ConLai)	
	else
	begin
		--set @temp1=round(@temp1,0)
		if (@temp0!='~' or @temp1!=0 or @temp2!=@ConLai)
		begin
		set @SoHoaDon=@temp0
		set @GiamGia=@temp1
		set @ConLai=round(@SauThue-@GiamGia,0)
		update DoanhThu set SoHoaDon=@SoHoaDon,TienGoc=@SumTongTien,SauThue=@SauThue,GiamGia=@GiamGia,ConLai=@ConLai where Ngay=@StepNgay and Buoi=@StepBuoi
		end
	end	
	set @temp0=null
	set @temp1=null
	set @temp2=null
	fetch next from cur_HD into @Ngay,@TongTien,@Buoi
end
close cur_HD
deallocate cur_HD
go

exec usp_DoanhThuMoi
go

create proc usp_LoadHoaDonTheoMaDT
@MaDT int
as
declare @NgayDT datetime, @BuoiDT nvarchar(50), @BuoiDT2 int
select @NgayDT=Ngay,@BuoiDT=Buoi from DoanhThu where MaDT=@MaDT
if(@BuoiDT=N'Sáng')
	set @BuoiDT2=0
else if(@BuoiDT=N'Tối')
	set @BuoiDT2=1	
select*from HoaDon where Ngay=@NgayDT and Buoi=@BuoiDT2
go

--exec usp_LoadHoaDonTheoMaDT 4
--go
*/